package com.example.idlefish.MyAccount

import android.annotation.*
import android.content.Context
import android.os.*
import android.util.Log
import android.view.*
import android.widget.Toast
import androidx.fragment.app.*
import androidx.lifecycle.ViewModelProvider
import com.example.idlefish.MyAccount.contract.*
import com.example.idlefish.MyAccount.pojo.*
import com.example.idlefish.PublicModule.*
import com.example.idlefish.databinding.*
import com.fasterxml.jackson.databind.*
import com.google.gson.*
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import java.io.*


@SuppressLint("SetTextI18n")
class MyAccountMainFragment : Fragment() {
    private lateinit var binding: MyaccountMainBinding
    private val client = OkHttpClient()
    private val mapper = ObjectMapper()
    private var signInId = 0
    private val handler = Handler(Looper.getMainLooper())
    private val mediaType = "application/json;charset=utf-8".toMediaTypeOrNull()
    private val message = Message()
    private var userInfo: UserHomepageInfo? = null
    // private lateinit var myAccountInfoViewModel: MyAccountViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = MyaccountMainBinding.inflate(layoutInflater)


        userInfo = arguments?.getSerializable("userInfo") as UserHomepageInfo?

        // 恢复 savedInstanceState 中的 userInfo, 切换到其他页面再切回来时还可以显示
        savedInstanceState?.let {
            userInfo = it.getSerializable("userInfo") as UserHomepageInfo?
            if (userInfo != null) {
                binding.nickname.text = userInfo?.nickname
                binding.avatar.setImageBitmap(UserConst.avatarImage)
                binding.collectionNum.text = userInfo?.collectionIds?.size.toString()
                binding.browseNum.text = userInfo?.browseHistoryIds?.size.toString()
                binding.followNum.text = userInfo?.followToIds?.size.toString()
            }
        }


        if (UserConst.id < 0) {
            loginLauncher.launch(123)
        }



        // 个人主页
        binding.toHomePage.setOnClickListener {
            myHomepageLauncher.launch(userInfo)
        }

        // 前往设置信息
        binding.settingButton.setOnClickListener {
            meInformationLauncher.launch(userInfo)
        }

        // 前往我的收藏
        binding.toCollection.setOnClickListener {
            meCollectionLauncher.launch(userInfo?.id)
        }

        // 前往浏览历史
        binding.toBrowse.setOnClickListener {
            meBrowseHistoryLauncher.launch(userInfo?.id)
        }


        // 前往关注列表
        binding.follow.setOnClickListener {
            meFollowLauncher.launch(userInfo?.id)
        }

        // 前往我发布的
        binding.myPost.setOnClickListener {
            myPostLauncher.launch(userInfo)
        }

        // 前往我卖出的
        binding.mySold.setOnClickListener {
            mySoldLauncher.launch(userInfo)
        }

        // 前往我买到的
        binding.myPurchased.setOnClickListener {
            myPurchasedLauncher.launch(userInfo)
        }


        // ai
        binding.toAI.setOnClickListener {
            chatLauncher.launch(Unit)
        }

        // 退出


        return binding.root
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        // 保存 userInfo 到 savedInstanceState
        outState.putSerializable("userInfo", userInfo)
    }

    private val loginLauncher = registerForActivityResult(LoginContract()) { result ->
        if (result != null) {
            userInfo = result
        }
        binding.nickname.text = UserConst.nickname
        binding.avatar.setImageBitmap(UserConst.avatarImage)
        // 收藏, 历史浏览. 关注
        binding.followNum.text = result?.followToIds?.size.toString()
        binding.collectionNum.text = result?.collectionIds?.size.toString()
        binding.browseNum.text = result?.browseHistoryIds?.size.toString()
    }


    // 设置信息
    private val meInformationLauncher = registerForActivityResult(MeInformationContract()) { result ->
            if (result != null) {
                binding.avatar.setImageBitmap(UserConst.avatarImage)
                binding.nickname.text = result.nickname
                userInfo!!.nickname = result.nickname.toString()
                userInfo!!.intro = result.intro.toString()
                userInfo!!.sex = result.sex ?: 1
                userInfo!!.avatar = result.avatar!!

            }

            Log.i("修改信息返回结果", "$result")
    }




    private val meCollectionLauncher = registerForActivityResult(MeCollectionContract()) { result ->
    }

    private val meBrowseHistoryLauncher = registerForActivityResult(MeBrowseHistoryContract()) { result ->

    }

    private val meFollowLauncher = registerForActivityResult(MeFollowContract()) { result ->
    }

    private val myPostLauncher = registerForActivityResult(MyPostContract()) { result ->
    }

    private val mySoldLauncher = registerForActivityResult(MySoldContract()) { result ->

    }

    private val myHomepageLauncher = registerForActivityResult(MyHomepageContract()) { result ->

    }

    private val myPurchasedLauncher = registerForActivityResult(MyPurchasedContract()) { result ->

    }

    private val chatLauncher = registerForActivityResult(ChatContract()) { result ->

    }

    /**
     * 我的收藏
     * 历史浏览
     * 我的关注
     */
    /*    fun toCollectionPage() {
             meFollowLauncher.launch(userInfo.collectionIds)
        }*/

    /*    fun toBrowseHistoryPage(view: View) {
            meBrowseHistoryLauncher.launch(UserConst.USERID)

        }*/


}